﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
//using Microsoft.Office.Tools.Excel;
namespace Bug_Speed
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.IsMdiContainer = true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImportFromExcel2003(object sender, EventArgs e)
        {
            try
            {

                Stream myStream = null;//myStream

                OpenFileDialog openFileDialog1 = new OpenFileDialog();

                openFileDialog1.InitialDirectory = "c:\\";
                openFileDialog1.Filter = "txt files (*.txt)|*.txt|Excel(*.xls)|*.xls";
                openFileDialog1.FilterIndex = 2;
                openFileDialog1.RestoreDirectory = true;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        if ((myStream = openFileDialog1.OpenFile()) != null)
                        {
                            ReadExcelToDisplay(openFileDialog1.FileName);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                    }
                }

            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void ReadExcelToDisplay(string fileName)
        {
            MessageBox.Show(fileName);

            #region "废弃"
            //Microsoft.Office.Interop.Excel.Application oXLAPP;
            //Microsoft.Office.Interop.Excel.Workbook oXLBook;

            //oXLAPP = new Microsoft.Office.Interop.Excel.Application();

            //oXLBook = oXLAPP.Workbooks.Open(fileName, 0, true, 5,
            //        "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false,
            //        0, true, true, false);

            //Microsoft.Office.Interop.Excel.Sheets sheets = oXLBook.Worksheets;
            //Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(2);
            //System.Array myvalues = null;
            //string[] strArray;
            //for (int i = 1; i <= 1; i++)
            //{
            //    Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "J" + i.ToString());
            //    myvalues = (System.Array)range.Cells.Value2;
            //    strArray = ConvertToStringArray(myvalues);
            //}

            #endregion

            #region "定义进度条"
            EventProcessArg epr = new EventProcessArg();
            epr.CurrentProcessStepsNumber = 1;
            epr.Maximum = 2000;
            epr.Minimum = 0;
            ProcessEventSource pe = new ProcessEventSource();
            ProcessBar pb = new ProcessBar(pe);
            pb.Show();
            pb.StartPosition = FormStartPosition.CenterScreen;
            #endregion

            //read the data from excel file and writ
            string fileLocation = fileName;
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"");
            ProcessBarStatus(100, pb, epr, pe);

            OleDbDataAdapter da = new OleDbDataAdapter();
            ProcessBarStatus(300, pb, epr, pe);
            DataTable dt = new DataTable();
            ProcessBarStatus(500, pb, epr, pe);
            da.SelectCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
            ProcessBarStatus(700, pb, epr, pe);
            da.Fill(dt);  // We could be filling a DataSet too
            ProcessBarStatus(900, pb, epr, pe);

            //System.Threading.Thread.Sleep(1000);
            conn.Dispose();
            ProcessBarStatus(1000, pb, epr, pe);

            da.Dispose();
            ProcessBarStatus(1300, pb, epr, pe);
            dt.Dispose();
            ProcessBarStatus(1500, pb, epr, pe);

            //Display bugs in buglist form.
            FrmBugList buglist = new FrmBugList();
            ProcessBarStatus(1800, pb, epr, pe);

            //System.Threading.Thread.Sleep(1000);
            buglist.ExcelData = dt;
            buglist.WindowState = FormWindowState.Maximized;
            buglist.MdiParent = this;
            buglist.Show();
            ProcessBarStatus(2000, pb, epr, pe);
            //System.Threading.Thread.Sleep(1000);
            pb.Hide();
            pb.Dispose();

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateTestCase(object sender, EventArgs e)
        {
            FrmTestCase tsc = new FrmTestCase();
            tsc.Operation = FrmTestCase.OpAttribute.ADD;
            tsc.WindowState = FormWindowState.Maximized;
            tsc.StartPosition = FormStartPosition.CenterScreen;
            tsc.MdiParent = this;
            tsc.Show();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FindCase(object sender, EventArgs e)
        {
            frmSearch fs = new frmSearch();
            fs.WindowState = FormWindowState.Normal;
            fs.StartPosition = FormStartPosition.CenterScreen;
            fs.ShowDialog();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="steps"></param>
        /// <param name="pb"></param>
        /// <param name="epr"></param>
        /// <param name="pe"></param>
        private void ProcessBarStatus(int steps, ProcessBar pb, EventProcessArg epr, ProcessEventSource pe)
        {
            pb.Select();
            epr.CurrentProcessStepsNumber = steps;
            System.Threading.Thread.Sleep(1);
            pe.OnEventCurrentSteps(epr);
         
        }

    }
}
